(function(root, $) {
    var urlParams;
    var first_url_key = 'UDESK_first_url';
    var first_access_key = 'UDESK_first_access';

    function parseUrlParams(url) {
        var obj = {},
            arr;
        if (!url) return obj;

        arr = url.match(new RegExp("[\?\&][^\?\&]+=[^\?\&]+", "g"));
        if (arr) {
            for (var i = 0, l = arr.length; i < l; i++) {
                var _obj, key, val;
                try {
                    _obj = arr[i].substring(1).split('=');
                    key = decodeURIComponent(_obj[0]);
                    val = decodeURIComponent(_obj[1]);
                    obj[key] = val;
                } catch (e) {
                    _obj && _obj[0] && (obj[_obj[0]] = _obj[1] || '');
                }
            }
        }
        return obj;
    }

    function urlAppendSuffix(url) {
        var first_url = urlParams.first_url || localStorage[first_url_key] || '';
        var first_access = urlParams.first_access || localStorage[first_access_key] || '';

        if (first_url || first_access) {
            url = url + (/\?/.test(url) ? '&' : '?') + $.param({
                first_url: first_url,
                first_access: first_access
            });
        }

        return url;
    }

    function goRegisterPage(url) {
        url = url.replace(/\/\/www\./, '//cb.');
        location.href = urlAppendSuffix(url);
    }

    function bindAllRegister() {
        $(document).on('click', 'a[href*="/register"]', function(ev) {
            goRegisterPage(this.href);
            ev.preventDefault();
        });
    }

    function openPage(pagePath, data) {
        var url = 'http://www.udesk.cn/' + pagePath;
        url += (data ? '?' + $.param(data) : '');
        url = urlAppendSuffix(url);

        window.open(url, 'newwindow');
    }

    function openRegisterPage(data) {
        openPage('register', data);
    }

    /**
        尝试跳转到移动端
    */
    function tryGoMobilePage() {
        var url = location.href;

        if (/(android|ios|iphone|ipod|ipad)/i.test(navigator.userAgent)) {
            url = url.replace('//www.', '//m.');
            location.href = urlAppendSuffix(url);
        }
    }

    urlParams = parseUrlParams(location.href);

    // if (urlParams.first_url) {
    //     localStorage[first_url_key] = urlParams.first_url;
    // }
    // if (urlParams.first_access) {
    //     localStorage[first_access_key] = urlParams.first_access;
    // }

    root.parseUrlParams = parseUrlParams;
    root.goRegisterPage = goRegisterPage;
    root.bindAllRegister = bindAllRegister;
    root.tryGoMobilePage = tryGoMobilePage;
    root.openPage = openPage;
    root.openRegisterPage = openRegisterPage;
})(this, jQuery);

$(function() {
    var sendEvent = function(target) {
        target = $(target);
        var values = (target.attr("ga-event") || "").split(",");
        if (values.length > 0) {
            ga('send', 'event', values[0], values[1] || "", values[2] || "", values[3] || undefined);
        }
    };

    if (window.ga) {
        sendEvent(document.body);

        $(document).on("click", "[ga-event]", function() {
            sendEvent(this);
        });
    }

    bindAllRegister();
});